import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
// import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

class Photopreview extends StatefulWidget {
  const Photopreview({super.key});

  @override
  State<Photopreview> createState() => _PhotopreviewState();
}

class _PhotopreviewState extends State<Photopreview> {
  List imageList = [];
  int curIndex = 0;
  bool isAvatar = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    imageList = Get.arguments['imageList'];
    curIndex = Get.arguments['curIndex'];
    isAvatar = Get.arguments['isAvatar'] ?? false;
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Get.back();
      },
      child: Scaffold(
        backgroundColor: Colors.black,
        body: Stack(
          children: [
            Center(
              child: Hero(
                tag: "img$curIndex",
                child: PhotoViewGallery.builder(
                  itemCount: imageList.length,
                  pageController: PageController(initialPage: curIndex),
                  onPageChanged: (value) {
                    setState(() {
                      curIndex = value;
                    });
                  },
                  builder: ((cotext, index) {
                    return PhotoViewGalleryPageOptions(
                      // imageProvider: NetworkImage(imageList[index]),
                      imageProvider: isAvatar
                          ? NetworkImage(imageList[index])
                          : FileImage(File(imageList[index])),
                    );
                  }),
                ),

                // child: PhotoView(
                //   imageProvider: NetworkImage(Get.arguments['imageUrl']),
                // )
              ),
            ),
            Align(
              alignment: const Alignment(0, 0.9),
              child: Text(
                isAvatar && imageList.length == 1
                    ? ""
                    : "${curIndex + 1} / ${imageList.length}",
                style: const TextStyle(fontSize: 20, color: Colors.white54),
              ),
            )
          ],
        ),
      ),
    );
  }
}
